package web;

import pojo.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //调用UserService.login()登入处理业务
        User loginUser = userService.login(new User(username, password, null));
        if (loginUser == null) {
            System.out.println("登入失败");
            //调回登入界面
            //把错误信息，和回显的表单项信息，保存到request域中
            request.setAttribute("msg", "用户名或密码错误");
            request.setAttribute("username", username);

            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        } else {
            System.out.println("登入成功");
            //跳转到login_success.html页面
            request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request, response);
        }
    }

}
